package com.iflytek.jobs.admin.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iflytek.jobs.service.domain.JobDelivery;
import com.iflytek.jobs.service.service.IJobDeliveryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 简历投递记录Controller
 * 
 * @author chenhs
 * @date 2023-04-26
 */
@RestController
@RequestMapping("/jobs/jobdelivery")
public class JobDeliveryController extends BaseController
{
    @Autowired
    private IJobDeliveryService jobDeliveryService;
    // 添加 ServletContext 注入

    /**
     * 查询简历投递记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(JobDelivery jobDelivery)
    {
        startPage();
        jobDelivery.setMapParams();
        List<JobDelivery> list = jobDeliveryService.selectJobDeliveryList(jobDelivery);
        return getDataTable(list);
    }


    /**
     * 获取简历投递记录详细信息
     */
    @GetMapping(value = "/{jdId}")
    public AjaxResult getInfo(@PathVariable("jdId") Integer jdId)
    {
        return success(jobDeliveryService.selectJobDeliveryByJdId(jdId));
    }

    /**
     * 新增简历投递记录
     */
    @PostMapping("/add")
    @PreAuthorize("@ss.notRole('demo')")
    public AjaxResult add(@RequestBody JobDelivery jobDelivery)
    {
    	jobDelivery.setCreateBy(super.getUsername());
    	int i = jobDeliveryService.insertJobDelivery(jobDelivery);
    	if(i==-1) {
    		return AjaxResult.warn("你的简历没有指导记录，无法投递岗位");
    	}else if(i==-2) {
    		return AjaxResult.warn("你的已投递过该岗位");
    	}else {
    		return toAjax(i);    		
    	}
    }

    /**
     * 修改简历投递记录
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.notRole('demo')")
    public AjaxResult edit(@RequestBody JobDelivery jobDelivery)
    {
        return toAjax(jobDeliveryService.updateJobDelivery(jobDelivery));
    }

    /**
     * 删除简历投递记录
     */
	@GetMapping("/delete/{jdId}")
    @PreAuthorize("@ss.notRole('demo')")
    public AjaxResult remove(@PathVariable Integer jdId)
    {
		int i = jobDeliveryService.deleteJobDeliveryByJdId(jdId);
		if(i==-1) {
			return AjaxResult.warn("该简历已经发往企业，无法撤回");
		}else {
			return toAjax(i);
		}
    }
	
	
	@GetMapping("/batchDownloadResume/{jdIds}")
    @PreAuthorize("@ss.notRole('demo')")
	public void batchDownloadResume(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer[] jdIds) throws IOException {
        String basePath = request.getScheme() + "://" + 
        request.getServerName() + ":" + 
        request.getServerPort();
		byte[] data = jobDeliveryService.download(basePath,jdIds);
		this.getZip(response, data);
		jobDeliveryService.batchUpdateDownloadStatus(jdIds);
	}
	
	/**
     * 生成zip文件
     */
    private void getZip(HttpServletResponse response, byte[] data) throws IOException
    {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"resume_"+System.currentTimeMillis()+".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
	
	
}
